<?php
namespace app\admin\controller;
use app\common\controller\AdminController;


class Files extends AdminController {

    //附件
    public function uploadAppendix(){

        $files  = new \app\common\model\Files();
        $res = $files->uploadAppendix();
        if($res['status']){
            $this->setActionLog('上传附件',$this->admin_info['name'].'上存了附件');
        }
        return $res;

    }

    public function uploadAppendixCopy(){

        $files  = new \app\common\model\Files();
        $res = $files->uploadAppendix('appendix_copy');
        if($res['status']){
            $this->setActionLog('上传附件',$this->admin_info['name'].'上存了附件');
        }
        return $res;

    }

    public function uploadVideo()
    {
        $result = error_code(10035);
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 移动到框架应用根目录/uploads/ 目录下
        $maxSize  = config('yitong.file_size');
        $filePath = ROOT_PATH . 'public';
        if (!is_dir($filePath)) {
            @mkdir($filePath);
        }

        try {
            validate(
                [
                    'file' => [
                        // 限制文件大小(单位b)，这里限制为4M
                        'fileSize' => $maxSize,
                        // 限制文件后缀，多个后缀以英文逗号分割
                        'fileExt'  => 'mp4'
                    ]
                ],
                [
                    'file.fileSize' => '文件太大',
                    'file.fileExt' => '不支持的文件后缀',
                ]
            )->check(['file' => $file]);

            $savename = \think\facade\Filesystem::disk('public')->putFile( 'video', $file);
            $name = str_replace('.'.$file->getOriginalExtension(),'',$file->getOriginalName());
            $url                = '/storage/'.$savename;
            $iData['type']      = 'local';
            $iData['file_type'] = 'video';
            $iData['name']      = $name;
            $iData['url']       = $url;
            $iData['ctime']     = time();
            $iData['path']      = $filePath.'/storage/'.$savename;
            $files              = new \app\common\model\Files();
            if ($files->strict(false)->insert($iData)) {
                $result['data']   = $iData;
                $result['status'] = true;
                $result['msg']    = '上传成功';
                return $result;
            }

        }catch (\think\exception\ValidateException $e){
            // 上传失败获取错误信息
            $result['msg'] = $e->getError();
            return $result;
        }
    }

    public function getCoverImage($fileUrl){
        $result = array();
        if(!empty($fileUrl)){
            $filePath = str_replace('http://img.baidu.cn','/data/images/',$fileUrl);
            if(is_file($filePath)){
                $result = $this->execCommandLine($filePath);
            }
        }
        return $result;
    }

    public function execCommandLine($file){
        $result = array();
        $pathParts = pathinfo($file);
        $filename = $pathParts['dirname'].'/'.$pathParts['filename'].'_';
        $item = array(8,15,25);
        foreach ($item as $k=>$v){
            $destFilePath = $filename.$v.'.jpg';
            $command = 'usr/bin/ffmpeg -i {$file} -y -f image2 -ss {v} -vframes 1 -s 640*360 {$destFilePath}';
            exec($command);
            $destUrlPath = str_replace('/data/images/','http://img.baidu.cn/',$destFilePath);
            $selected = $k == 0 ? "1" : "0";
            array_push($result,array($destUrlPath,$selected));
        }
        return $result;
    }



}
